package com.lintf.bot.rabbitmq.config;

import com.lintf.bot.rabbitmq.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author lintf
 */
@Configuration
public class RetryRabbitConfig {

    /**
     * ------------------- 延时队列相关 -------------------
     * @return
     */

    /**
     * 延时交换机
     */
    @Bean
    public DirectExchange ttlDelayExchangeRetry() {
        return new DirectExchange(Constants.DELAY_EXCHANGE);
    }

    /**
     * 延时交换机队列
     */
    @Bean
    public Queue ttlDelayExchangeQueueRetry() {
        Map<String, Object> map = new HashMap<String, Object>();
        //队列中所有消息5秒后过期
        //map.put("x-message-ttl", 1000 * 60 * 5);
        //过期后进入死信队列
        map.put("x-dead-letter-exchange", Constants.DEAD_LETTER_EXCHANGE);
        return new Queue(Constants.DELAY_EXCHANGE_QUEUE, false, false, false, map);
    }

    /**
     * Fanout交换机和productQueue绑定
     */
    @Bean
    public Binding bindTtlExchangeAndQueueRetry() {
        return BindingBuilder.bind(ttlDelayExchangeQueueRetry()).to(ttlDelayExchangeRetry()).with(Constants.DELAY_EXCHANGE_ROUTE_KEY);
    }

    /**
     * ------------------- 死信队列相关 -------------------
     */

    /**
     * fanout死信交换机
     *
     * @return
     */
    @Bean
    public FanoutExchange deadLetterExchange() {
        return new FanoutExchange(Constants.DEAD_LETTER_EXCHANGE);
    }

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(Constants.DEAD_LETTER_EXCHANGE_QUEUE);
    }

    /**
     * 死信队列和死信交换机绑定
     *
     * @return
     */
    @Bean
    public Binding deadLetterBind() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange());
    }


    /**
     * ------------------- 业务队列相关 -------------------
     */

    /**
     * fanout业务交换机
     *
     * @return
     */
    @Bean
    public DirectExchange myExchange() {
        return new DirectExchange(Constants.MY_EXCHANGE);
    }

    /**
     * 通用通知队列
     *
     * @return
     */
    @Bean
    public Queue notificationQueue() {
        return new Queue(Constants.NOTIFICATION_QUEUE);
    }

    /**
     * 通用通知队列和业务交换机绑定
     *
     * @return
     */
    @Bean
    public Binding notificationBind() {
        return BindingBuilder.bind(notificationQueue()).to(myExchange()).with(Constants.NOTIFICATION_ROUTE_KEY);
    }

    /**
     * 消息通知队列
     *
     * @return
     */
    @Bean
    public Queue likeNotificationQueue() {
        return new Queue(Constants.LIKE_NOTIFICATION_QUEUE);
    }

    /**
     * 消息通知队列和业务交换机绑定
     *
     * @return
     */
    @Bean
    public Binding likeNotificationBind() {
        return BindingBuilder.bind(likeNotificationQueue()).to(myExchange()).with(Constants.LIKE_NOTIFICATION_ROUTE_KEY);
    }

    /**
     * 待办通知队列
     *
     * @return
     */
    @Bean
    public Queue itemNotificationQueue() {
        return new Queue(Constants.ITEM_NOTIFICATION_QUEUE);
    }

    /**
     * 待办通知队列和业务交换机绑定
     *
     * @return
     */
    @Bean
    public Binding itemNotificationBind() {
        return BindingBuilder.bind(itemNotificationQueue()).to(myExchange()).with(Constants.ITEM_NOTIFICATION_ROUTE_KEY);
    }
}
